package com.jic.order.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.entity.OrderMerge;
import com.jic.order.entity.OrderSplit;
import com.jic.order.manage.request.OrderMergeRequest;
import com.jic.order.manage.request.OrderSplitEditRequest;
import com.jic.order.manage.response.OrderMergeResponse;
import com.jic.order.service.OrderMergeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j

@Api(value = "订单合单规则", tags = {"订单合并规则"})
@RequestMapping(value = "orderMerge")
@RestController
public class OrderMergeController {

	@Autowired
	OrderMergeService orderMergeService;

	@ApiOperation(value = "获取所有订单合并信息")
	@ResponseBody
	@PostMapping(value = "/queryPage")
	public RestResult<PageResult<OrderMergeResponse>> queryPage(@RequestBody OrderMergeRequest form) {
		log.info("开始调用订单合并分页，{}", JSON.toJSONString(form));
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderMerge orderMerge = new OrderMerge();
		BeanUtils.copyProperties(form, orderMerge);
		DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
		if (deptType.getCode() != 0) {
			orderMerge.setDeptId(form.getDeptId());
		}
		PageResult<OrderMerge> result = orderMergeService.queryPage(orderMerge, page);
		List<OrderMergeResponse> orderMergeResponseList = result.getRows().stream().map(temp -> {
			OrderMergeResponse orderMergeResponse = new OrderMergeResponse();
			BeanUtils.copyProperties(temp, orderMergeResponse);
			return orderMergeResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderMergeResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderMergeResponseList);
		pageResult.setTotal(orderMergeResponseList.size());
		return RestResult.success(pageResult);

	}

	@ApiOperation(value = "订单合并规则新增编辑")
	@ResponseBody
	@PostMapping(value = "/editOrderMerge")
	public RestResult editOrderSplit(@RequestBody OrderMergeRequest form) {
		log.info("开始调用订单合并新增编辑，{}", JSON.toJSONString(form));
		RestResult restResult = new RestResult<>();
		try {
			OrderMerge orderMerge = new OrderMerge();
			BeanUtils.copyProperties(form, orderMerge);
			orderMerge.setDeptId(form.getAdminOperate().getDeptId());
			orderMergeService.editOrderMerge(orderMerge);
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("editOrderSplit========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "批量停用订单合并")
	@ResponseBody
	@PostMapping(value = "/editOrderMergeStatus")
	public RestResult editOrderMergeStatus(@RequestBody OrderMergeRequest form) {
		log.info("开始调用批量停用订单合并，{}", form.toString());
		RestResult restResult = new RestResult<>();
		try {
			orderMergeService.editOrderMergeStatus(form.getIds());
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("editOrderSplitStatus========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "删除订单合并规则")
	@ResponseBody
	@PostMapping(value = "/deleteOrderMerge")
	public RestResult deleteOrderMerge(@RequestBody OrderMergeRequest form) {
		log.info("开始调用删除订单拆分规则，{}", form.toString());
		RestResult restResult = new RestResult<>();
		try {
			orderMergeService.delete(form.getIds());
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("deleteOrderSplit========》", e);
		}
		return restResult;

	}


}
